<?php

require_once 'controller.php';
require_once 'netcontrol_dao.php';

class netcontrol_ctl extends controller
{
	private $contracts;
	protected $netcontrol_dao;

	public function __construct()
	{
		$this->netcontrol_dao = new netcontrol_dao();
		$this->contracts = $this->netcontrol_dao->fetch_contracts(date('l'),date('H:i:s'));
	}

	function refresh_dhcp()
	{
		$config = config::get_instance();
		$out_iface = $config->iface;
		$commands = array();
		$dhcpd_conf = stripcslashes($config->dhcpd_header);

		foreach ($this->contracts as $contract) {
			if($contract->ip != '' && $contract->mac != '') {
				$dhcpd_conf .= "\n";
				$dhcpd_conf .= "host ".$contract->username.$contract->handle." {\n";
				$dhcpd_conf .= "\thardware ethernet ".strtoupper($contract->mac).";\n";
				$dhcpd_conf .= "\tfixed-address ".$contract->ip.";\n";
				$dhcpd_conf .= "}\n";
			}
		}
		$file = fopen('/etc/dhcp3/dhcpd.conf','w');
		fwrite($file, $dhcpd_conf);
		fclose($file);
		return '<netcontrol><dhcp>'.exec('sudo /etc/init.d/dhcp3-server restart').'</dhcp></netcontrol>';
	}
	
	function refresh_iptables()
	{
		$config = config::get_instance();
		$out_iface = $config->iface;
		$commands = array();
		$iptables = '';
		$iptables_nat = '';
		$iptables_mangle = '';

		foreach ($this->contracts as $contract) {
			if ($contract->ip != ''
			&& $contract->mac != ''
			&& $contract->status == 'L'
			&& $contract->scheduled
			&& ($contract->active || !$contract->auth)) {
				$iptables_nat .= '-I PREROUTING -s '.$contract->ip.' -p tcp --dport 80 ';
				if ($contract->auth) {
					$iptables_nat .= '-m mac --mac-source '.strtoupper($contract->mac);
				}
				if ($contract->proxy_port) {
					$iptables_nat .= " -j REDIRECT --to-port ".$contract->proxy_port."\n";
				} else {
					$iptables_nat .= " -j ACCEPT\n";
				}

				$iptables_mangle .= "-A PREROUTING -s ".$contract->ip." -j MARK --set-mark 3".$contract->handle."\n";

				$iptables_filter .= "-A FORWARD -s ".$contract->ip;
				if ($contract->auth) {
					$iptables_filter .= " -m mac --mac-source ".strtoupper($contract->mac);
				}
				$iptables_filter .= " -j ACCEPT\n";
				$iptables_filter .= "-A FORWARD -d ".$contract->ip." -j ACCEPT\n";

				$iptables_filter .= "-A INPUT -s ".$contract->ip;
				if ($contract->auth) {
					$iptables_filter .= ' -m mac --mac-source '.strtoupper($contract->mac);
				}
				$iptables_filter .= " -j ACCEPT\n";

				$iptables_filter .= "-A OUTPUT -s ".$contract->ip." -j ACCEPT\n";
			}
		}
		$iptables = stripcslashes($config->iptables);
		$iptables = str_replace('{vulpes_nat}',$iptables_nat,$iptables);
		$iptables = str_replace('{vulpes_mangle}',$iptables_mangle,$iptables);
		$iptables = str_replace('{vulpes_filter}',$iptables_filter,$iptables);
		$file_iptables = fopen('/etc/firewall','w');
		fwrite($file_iptables,$iptables);
		fclose($file_iptables);
		exec('sudo /sbin/iptables-restore < /etc/firewall');
	}

	function refresh_tc()
	{
		$config = config::get_instance();
		$out_iface = $config->iface;
		$commands = array();
		$used_interfaces = array();

		$commands[] = 'sudo /sbin/tc qdisc del dev '.$config->iface.' root';
		$commands[] = 'sudo /sbin/tc qdisc add dev '.$config->iface.' root handle 1: htb default 3';
	
		foreach ($this->contracts as $contract) {
			if($contract->ip != '' && $contract->mac != '') {
				if (!$used_interfaces[$contract->iface]) {
					$commands[] = 'sudo /sbin/tc qdisc del dev '.$contract->iface.' root';
					$commands[] = 'sudo /sbin/tc qdisc add dev '.$contract->iface.' root handle 1: htb default 3';
					$used_interfaces[$contract->iface] = true;
				}
				if ($contract->status == 'L' && ($contract->active || !$contract->auth)) {
					$commands[] = 'sudo /sbin/tc class add dev '.$out_iface.' parent 1:0 classid 1:1'.$contract->handle.' htb rate '.$contract->upload.'kbit ceil 0 burst 15k';
					$commands[] = 'sudo /sbin/tc qdisc add dev '.$out_iface.' parent 1:1'.$contract->handle.' handle 1'.$contract->handle.': sfq perturb 10';
					$commands[] = 'sudo /sbin/tc class add dev '.$contract->iface.' parent 1:0 classid 1:2'.$contract->handle.' htb rate '.$contract->download.'kbit ceil 0 burst 15k';
					$commands[] = 'sudo /sbin/tc qdisc add dev '.$contract->iface.' parent 1:2'.$contract->handle.' handle 2'.$contract->handle.': sfq perturb 10';
					$commands[] = 'sudo /sbin/tc filter add dev '.$out_iface.' parent 1:0 protocol ip prio 1 handle 3'.$contract->handle.' fw flowid 1:1'.$contract->handle;
					$commands[] = 'sudo /sbin/tc filter add dev '.$contract->iface.' parent 1:0 protocol ip prio 1 u32 match ip dst '.$contract->ip.' flowid 1:2'.$contract->handle;
				}
			}
		}

		foreach ($commands as $command) {
			exec($command, $messages, $error);
			exec('echo "'.$command.'" >> /var/log/vulpes_tc.log');
		}
	}
	
}

?>